#include "Model.h"
#include "ModelMAYA.h"
#include "ModelMS3D.h"
#include "ModelMD2.h"
#include "ModelMD5.h"
#include "../Shapes3D/Point3D.h"
#include "../GameLib.h"
#include "../Logger.h"
#include <assert.h>

bool _ModelDef::load(const char *filename){assert(!"No loading implementation made!");return false;}
void _ModelDef::draw(int &index,double scale){assert(!"No drawing implementation made!");}
int _ModelDef::getFrameCount(){return 0;}
bool _ModelDef::hasFrames(){return false;}
void _ModelDef::draw()
{
	int index = 0;
	draw(index, 1.0);
}

Model::Model()
{
	index=0;
	model=0;
}

Model::Model(const char *filename)
{
	index=0;
	model=0;
	load(filename);
}

Model::~Model()
{
	if (model!=0){delete model;}
}

bool Model::load(const char *filename)
{
	Str ext;
	ext = filename;
	ext.keep(ext.rfind(".")+1, 0);
	ext.toLower();
	bool success=false;
	
	if (ext == "ms3d")
	{
		model = new _ModelMS3D();
	}
	else if (ext == "ma")
	{
		model = new _ModelMAYA();
	}
	else if (ext == "md2")
	{
		model = new _ModelMD2();
	}
	else if (ext == "md5")
	{
		model = new _ModelMD5();
	}
	else
	{
		errLog.print(Str()<<"Do not know how to interperate the type: "<<ext<<" as a model!");
	}
	
	if (model!=0)
	{
		success = model->load(filename);
	}
	if (success)
	{
		this->filename=filename;
	}
	else
	{
		mainLog.print(Str()<<"Could not load the model \""<<filename<<"\"");
		delete model;
		model=0;
	}
	return success;
}

Model& Model::draw(Point3D pos,double rotX,double rotY,double rotZ)
{
	if (!model){return *this;}
	glPushMatrix();
	gameLib.pushColor();
	gameLib.setColor(C_WHITE);
	glTranslated(pos.getX(),pos.getY(),pos.getZ());
	glRotated(rotX,1,0,0);
	glRotated(rotY,0,1,0);
	glRotated(rotZ,0,0,1);
	model->draw(index,0.1);
	gameLib.popColor();
	glPopMatrix();
	return *this;
}